//
//  MOAppDelegate.m
//  MOPush
//
//  Created by Alfie on 03/31/2021.
//  Copyright (c) 2021 Alfie. All rights reserved.
//

#import "MOAppDelegate.h"
#import <UserNotifications/UserNotifications.h>
/// 引入MOPush头文件
#import <MOPush/MOPushClient.h>

#define APPKEY @"yCwiJWJ6on"

@implementation MOAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    /// 初始化MOPushSDK
    [MOPushClient registerWithLaunchOptions:launchOptions appKey:APPKEY registerEntity:nil];
    /// Debug模式下可选择开启日志输出
    [MOPushClient setLogEnable:YES];
    return YES;
}


#pragma mark - token上报
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    [MOPushClient registerDeviceToken:deviceToken completion:^(BOOL granted, NSError *error) {
        if (granted) {
            NSLog(@"MOPush deviceToken上报成功");
        } else {
            NSLog(@"MOPush deviceToken上报失败：%@", error);
        }
    }];
}


#pragma mark - 注册失败回调
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}


#pragma mark - iOS 7 Support - 静默推送
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"收到静默推送:%@", userInfo);
    completionHandler(UIBackgroundFetchResultNewData);
}


#pragma mark - iOS 10 Support - 前台收到推送回调
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0)){
    
    NSDictionary *userInfo = notification.request.content.userInfo;
    NSLog(@"前台收到推送:%@", userInfo);
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}


#pragma mark - iOS 10 Support - 通过点击推送打开应用的回调
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)) {
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    NSLog(@"点击推送:%@", userInfo);
}
    

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

@end
